//
//  ReadViewController.m
//  A Cup
//
//  Created by dllo on 15/4/21.
//  Copyright (c) 2015年 doll. All rights reserved.
//

#import "ReadViewController.h"
#import "AFHTTPRequestOperationManager.h"
#import "AFHTTPRequestOperation.h"
#import "Read.h"
#import "dataBaseHandle.h"
#define WIDTH self.view.frame.size.width

@interface ReadViewController ()
@property(nonatomic, retain)NSMutableArray *readArr;
@property(nonatomic, retain)UICollectionView *readCollectionView;
@property(nonatomic, retain)UIActivityIndicatorView *activity;
@end

@implementation ReadViewController
//初始化
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.readArr = [NSMutableArray array];
        self.title = @"阅读";
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.activity = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(WIDTH/2, 100, 30, 30)];
    [self.activity setCenter:CGPointMake(self.view.frame.size.width / 2, (self.view.frame.size.height - 113) / 2)]; // 指定进度轮中心点
    [self.activity setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
    [self.view addSubview:self.activity];
    [self.view bringSubviewToFront:self.activity];
    [self.activity startAnimating];
    [_activity release];
    
    [self creatView];
    [self PostData];
}

- (void)PostData
{
    if ([dataBaseHandle shareDB].state == 1) {
        //AFN解析POST数据
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"1",@"client",@"D8FDE94C-841F-4A4D-B5C4-AAD2472CEE85",@"deviceid",@"",@"auth",@"3.0.1",@"version", nil];
        [manager POST:@"http://api2.pianke.me/read/columns" parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSMutableDictionary *dic = responseObject;
            NSMutableDictionary *Dic = dic[@"data"];
            NSMutableArray *arr = Dic[@"list"];
            for (NSMutableDictionary *temp in arr) {
                Read *read = [[Read alloc] init];
                [read setValuesForKeysWithDictionary:temp];
                [self.readArr addObject:read];
            }
            [self.readCollectionView reloadData];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        }];
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"当前无网络，请检查网络后重新尝试" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
    }
}

- (void)creatView
{
    //瀑布流效果
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(WIDTH/3 - 25, WIDTH/3);
    //创建collectionview
     flowLayout.sectionInset = UIEdgeInsetsMake(30, 20, 20, 20);//设置Item上下左右边距
    
    self.readCollectionView = [[UICollectionView alloc] initWithFrame:[[UIScreen mainScreen] bounds] collectionViewLayout:flowLayout];
    self.readCollectionView.backgroundColor = [UIColor whiteColor];
    //设置代理
    self.readCollectionView.delegate =self;
    self.readCollectionView.dataSource =self;
    [self.view addSubview:self.readCollectionView];
    [self.view sendSubviewToBack:self.readCollectionView];
   [_readCollectionView release];
    [flowLayout release];
#pragma mark collectionView 采用注册的方式生成cell
    [self.readCollectionView registerClass:[ReadCell class] forCellWithReuseIdentifier:@"reuse"];
    
}

- (void)dealloc{
    [_readCollectionView release];
    [_readArr release];
    [_activity release];
    [super dealloc];
}

#pragma mark CollectionView 两个必须实现的方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (self.readArr.count > 0) {
        [self.activity stopAnimating];
    }
    return self.readArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ReadCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    cell.readlabel.text =[NSString stringWithFormat:@"%@%@",[self.readArr[indexPath.row] name],[self.readArr[indexPath.row]enname]];
    cell.readlabel.textAlignment = NSTextAlignmentCenter;
    NSURL *url = [NSURL URLWithString:[self.readArr[indexPath.row] coverimg]];
    [cell.readimage sd_setImageWithURL:url];
    cell.readimage.clipsToBounds = YES;
    cell.readimage.contentMode = UIViewContentModeScaleAspectFill;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    ReadDetialViewController *readDetial = [[ReadDetialViewController alloc] init];
    readDetial.titName = [self.readArr[indexPath.row] enname];
    readDetial.typenumber = [self.readArr[indexPath.row] type];
    [self.navigationController pushViewController:readDetial animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
